﻿namespace AllServer.Core;

public class FileLogServiceComponent : IServiceComponent
{
    public void Load(IServiceCollection services, ComponentContext componentContext)
    {
        services.AddFileLogging(options =>
        {
            options.WithTraceId = true;
            options.WriteFilter = (logMsg) =>
            {
                return logMsg.LogLevel == LogLevel.Information;
            };
            options.MessageFormat = (logMsg) =>
            {
                return logMsg.WriteArray(writer =>
                {
                    writer.WriteStringValue(DateTime.Now.ToString("O"));
                    writer.WriteStringValue(logMsg.LogLevel.ToString());
                    writer.WriteStringValue(logMsg.LogName);
                    writer.WriteNumberValue(logMsg.EventId.Id);
                    writer.WriteStringValue("【" + logMsg.TraceId + "】");
                    writer.WriteStringValue(logMsg.Message);
                    writer.WriteStringValue(logMsg.Exception?.ToString());
                });
                //ExceptionlessClient.Default.SubmitLog(msg, Exceptionless.Logging.LogLevel.Info);
                //return msg;
            };


        });
        services.AddFileLogging(() => "MyLogger", options =>
        {
            options.WithTraceId = true;
            options.WriteFilter = (logMsg) =>
            {
                return logMsg.LogLevel == LogLevel.Error;
            };
            options.MessageFormat = (logMsg) =>
            {
                return logMsg.WriteArray(writer =>
                {
                    writer.WriteStringValue(DateTime.Now.ToString("O"));
                    writer.WriteStringValue(logMsg.LogLevel.ToString());
                    writer.WriteStringValue(logMsg.LogName);
                    writer.WriteNumberValue(logMsg.EventId.Id);
                    writer.WriteStringValue("【" + logMsg.TraceId + "】");
                    writer.WriteStringValue(logMsg.Message);
                    writer.WriteStringValue(logMsg.Exception?.ToString());
                });
            };

        });
    }
}
